package com.pignest.hotel.ui.income;

import java.util.Date;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.service.IncomeService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.system.InComeUtil;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class ComeOnLivingWindow extends Window implements EventListener {

	/**
	 * 续住
	 */
	private static final long serialVersionUID = 2393203618200422988L;
	
	private IncomeService incomeService;
	private Living curLiving;
	private Label roomName;
    private Label customName;
    private Label livingTime;
    private Label storage;
    private Label used;
    private Label balance;
    private Datebox leaveDate;
    //private Label liveType;
    private Label payed;
  //  private Listbox leaveTime;
    private Textbox comeOnPay;
    
    private Button okBtn;
    private Button printBtn;
    private Button madeCardBtn;
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			roomName=(Label)getFellow("roomName");
			customName=(Label)getFellow("customName");
			livingTime=(Label)getFellow("livingTime");
			storage=(Label)getFellow("storage");
			used=(Label)getFellow("used");
			balance=(Label)getFellow("balance");
			leaveDate=(Datebox)getFellow("leaveDate");
			//leaveTime=(Listbox)getFellow("leaveTime");
			//liveType =(Label)getFellow("liveType");
			payed =(Label)getFellow("payed");
			comeOnPay=(Textbox)getFellow("comeOnPay");
			okBtn=(Button)getFellow("okBtn");
			printBtn=(Button)getFellow("printBtn");
			madeCardBtn=(Button)getFellow("madeCardBtn");
			init();
		}
	}
	public ComeOnLivingWindow()
	{
		incomeService =Services.getIncomeService();
		addEventListener("onCreate", this);
	}
	public void init()
	{
		if(this.curLiving==null)
			return ;
		roomName.setValue(curLiving.getRoom().getName());
		customName.setValue(curLiving.getCustormName());
		livingTime.setValue(DateUtil.toString(curLiving.getLiveTime()));
		leaveDate.setValue(curLiving.getExpectLeaveDate());
		InComeUtil.countInComeNoAdd(curLiving, storage, used, payed, balance);
		
	}
	public void save()
	{
		if(this.curLiving==null)
		{
			UIUtils.alert("无效的入住!");
			return ;
		}
		if(!Hotel.onDuty()){
			UIUtils.alert("你不在值班状态不能操作此业务!");
			return ;
		}
		if(curLiving.getLiveState().equals(LivingState.CHECKOUT.name()))
		{
			UIUtils.alert("当前状态已经结账了！");
			return ;
		}
		
		String price =comeOnPay.getValue();
		if(!Utils.checkPrice(price)){
			UIUtils.alert("请输入合法值，非负数,小数不能超过2位");
			return ;
		}
		
		Date leaveTime=leaveDate.getValue();
		StringBuffer msg = new StringBuffer();
		msg.append("你好！").append(curLiving.getCustormName()).append("\n");
		msg.append("续存:").append(price).append("\n");
		msg.append("延期到:").append(DateUtil.toString(leaveTime, "yyyy-MM-dd HH:mm")).append("\n");

		if(UIUtils.confirm(msg.toString())){
			TbAccounts ts = new TbAccounts();
			ts.setLiving(curLiving);
			ts.setType(InComeItemType.XCK.name());
			ts.setCounts(1);
			ts.setName(InComeItemType.XCK.getName());
			ts.setPrice(Utils.toPrice(price));
	        ts.setEndstate("否");
	        ts.setOptime(new Date());
	        ts.setOpUser(Hotel.getCurrentUser().getName());
	        ts.setPrice2(Utils.toPrice(price));
	        ts.setOpdate(DateUtil.currentDateToString(DateUtil.DEFAULT_DATE_FORMAT));
	        ts.setRemark(InComeItemType.XCK.getName());
	        curLiving.setStorage(curLiving.getStorage()+Utils.toPrice(price));
	        curLiving.setExpectLeaveDate(leaveTime);
	        curLiving.setGrantDate(leaveTime);
	        curLiving=incomeService.updateComeOnAccount(ts, curLiving);
	        okBtn.setDisabled(true);
	        printBtn.setDisabled(false);
	        madeCardBtn.setDisabled(false);
	        init();
	   }
	}
	public Living getCurLiving() {
		return curLiving;
	}
	public void setCurLiving(Living curLiving) {
		this.curLiving = curLiving;
	}

	

}
